#include "util.hh"

std::string curr_time()
{
  time_t rawtime;
  time(&rawtime);
  struct tm * timeinfo = localtime(&rawtime);
  char buff[80];
  strftime(buff, 80, "%c", timeinfo);
  return std::string(buff);
}

void err_msg(const std::string msg)
{
  std::cerr << "Error occurred at " << curr_time() << " : " << msg << std::endl;
}

void assert_msg(bool cond, const std::string msg)
{
  if (!cond)
  {
    err_msg(msg);
    exit(1);
  }
}

// count number of columns by 1st line
size_t num_columns(const char* file)
{
  std::ifstream ifs(file, std::ios::in);

  std::istreambuf_iterator<char> eos;
  std::istreambuf_iterator<char> it(ifs);

  size_t ret = 1;
  for (; it != eos && *it != '\n'; ++it)
    if (*it == '\t')
      ++ret;

  ifs.close();
  return ret;
}

size_t num_rows(const char* file)
{
  std::ifstream ifs(file, std::ios::in);

  std::istreambuf_iterator<char> eos;
  std::istreambuf_iterator<char> it(ifs);

  size_t ret = 0;
  for (; it != eos; ++it)
    if (*it == '\n')
      ++ret;

  ifs.close();
  return ret;
}

double trigamma(const double xorg)
{
   double p;
   int i;
   double x = xorg;
   x=x+6;
   p=1/(x*x);
   p=(((((0.075757575757576*p-0.033333333333333)*p+0.0238095238095238)
        *p-0.033333333333333)*p+0.166666666666667)*p+1)/x+0.5*p;
   for (i=0; i<6 ;i++)
   {
       x=x-1;
       p=1/(x*x)+p;
   }
   return(p);
//  return gsl_sf_psi_1(xorg);
}
